package net.cloudcodex.client.storyboard.player.handler;

import java.util.HashMap;
import java.util.Map;

import net.cloudcodex.client.storyboard.cdo.CharacterKeyCDO;
import net.cloudcodex.client.storyboard.player.PlayerBoardEventBus;
import net.cloudcodex.client.storyboard.player.presenter.PlayerBoardPresenter;
import net.cloudcodex.client.util.Container;

import com.mvp4g.client.annotation.EventHandler;
import com.mvp4g.client.event.BaseEventHandler;

/**
 * Initializer and cache of {@link PlayerBoardPresenter}.
 * 
 * @author ADaroussin
 */
@EventHandler
public class PlayerBoardEventHandler extends BaseEventHandler<PlayerBoardEventBus> {
	private final Map<CharacterKeyCDO, PlayerBoardPresenter> cache = new HashMap<CharacterKeyCDO, PlayerBoardPresenter>();

	public void onNewPlayerBoard(long campaignId, long characterId, Container<PlayerBoardPresenter> container) {
		final CharacterKeyCDO characterKey = new CharacterKeyCDO(campaignId, characterId);
		PlayerBoardPresenter playerBoardPresenter = cache.get(characterKey);
		if (playerBoardPresenter == null) {
			playerBoardPresenter = eventBus.addHandler(PlayerBoardPresenter.class);
			cache.put(characterKey, playerBoardPresenter);
			playerBoardPresenter.initPlayerBoard(campaignId, characterId);
		}
		container.set(playerBoardPresenter);
	}
}
